package com.itheima.utils;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import java.util.ResourceBundle;

//Jedis工具类
public class JedisUtils {
    private static int maxTotal;
    private static int maxIdel;
    private static String host;
    private static int port;
    private static JedisPoolConfig jpc;
    private static JedisPool jp;


    static {
        //读取配置文件
        ResourceBundle bundle = ResourceBundle.getBundle("jedis");
        //获取参数
        maxTotal = Integer.parseInt(bundle.getString("jedis.maxTotal"));
        maxIdel = Integer.parseInt(bundle.getString("jedis.maxIdle"));
        host = bundle.getString("jedis.host");
        port = Integer.parseInt(bundle.getString("jedis.port"));
        //Jedis连接池配置
        jpc = new JedisPoolConfig();
        jpc.setMaxTotal(maxTotal);
        jpc.setMaxIdle(maxIdel);
        jp = new JedisPool(jpc,host,port);

    }

    //获取连接方法
    public static Jedis getJedis(){
        Jedis jedis = jp.getResource();
        return jedis;
    }
}
